package com.hissy.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 哈希计算
 */
public class HashUtil {
    private static final byte[] compute(String content, String hashType){
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(hashType);
            return messageDigest.digest(content.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static final String computeMD5(String content) {
        return HexStringUtil.toHexString(compute(content, "MD5"));
    }

    public static final String computeSHA1(String content) {
        return HexStringUtil.toHexString(compute(content, "SHA1"));
    }

    public static final String computeSHA256(String content) {
        return HexStringUtil.toHexString(compute(content, "SHA-256"));
    }

    public static final String computeSHA384(String content) {
        return HexStringUtil.toHexString(compute(content, "SHA-384"));
    }

    public static final String computeSHA512(String content) {
        return HexStringUtil.toHexString(compute(content, "SHA-512"));
    }
}
